<?php
!defined('IN_SYSTEM') && exit('Access Denied!');

//缓存配置信息
function settings_recache() {
	global $_G;
	$content = array();
	$settings = $_G['db']->fetch('settings','*');
	if(is_array($settings)) {
		foreach($settings as $setting) {
			if($setting['type']) {
				$content[$setting['type']][$setting['name']] = $setting['value'];
			}else{
				if($setting['name'] == 'alias'){
					$content[$setting['name']] = unserialize($setting['value']);
				}else{
					$content[$setting['name']] = $setting['value'];
				}	
			}
		}
	}
	$_G['cache']->set('settings',$content,null);
}

//缓存分类
function category_recache() {
	global $_G;
	$content = array();
	$sql = "SELECT * FROM ".DB_PREFIX."category WHERE uid=0 ORDER BY listorder";
	$query = $_G['db']->query($sql);
	while ($row = $_G['db']->fetch_array($query)) {
		$count = $_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."article WHERE `status` < 4 AND cid = ".$row['cid']);
		$_G['db']->query("UPDATE ".DB_PREFIX."category SET `total` = '$count' WHERE cid = ".$row['cid']);
		$row['count'] = $count;
//		if ( $_G['settings']['seo']['rewrite'] == 1 ) {
//			$row['url'] = 'category/';
//			$row['url'] .= empty($row['urlname']) ? $row['cid'].'/' : rawurlencode($row['urlname']).'/';
//		} else {
//			$row['url'] = './index.php?cid='.$row['cid'];
//		}
		if ( !empty($row['redirect']) ) {
			$row['url'] = $row['redirect'];
		}
		$content[$row['cid']] = $row;
	}
	$_G['cache']->set('category',$content,null);
}

//缓存用户组
function usergroups_recache() {
	global $_G;
	$usergroups = $_G['db']->fetch('usergroups','*');
	foreach($usergroups as $usergroup) {
		$usergroup['access'] = unserialize($usergroup['access']);
		$_G['cache']->set('usergroup_'.$usergroup['gid'],$usergroup,null);
	}
}

//缓存日志归档
function archive_recache() {
	global $_G;
	$content = array();
	$limit = intval($_G['settings']['display']['archive_num']) ? ' LIMIT 0,'.intval($_G['settings']['display']['archive_num']) : '';
	$sql = "SELECT yearmonth,COUNT(*) AS num FROM ".DB_PREFIX."calendar GROUP BY yearmonth ORDER BY yearmonth DESC {$limit}";
	$result = $_G['db']->query($sql);
	while ($row = $_G['db']->fetch_array($result)) {
		$archive = array('yearmonth' => $row['yearmonth'],'year' => substr($row['yearmonth'],0,4),'month' => substr($row['yearmonth'],4),'count' => $row['num']);
		if ( $_G['settings']['seo']['rewrite'] == 1 ) {
			$archive['url'] = "archive/".$row['yearmonth']."/";
		} else {
			$archive['url'] = "./index.php?archive=".$row['yearmonth'];
		}
		$content[] = $archive;
	}
	$_G['cache']->set('archive',$content,null);
}

//缓存最新文章
function lastest_article_recache() {
	global $_G;
	$content = array();
	$limit = ' LIMIT 0,'.intval($_G['settings']['display']['sidebar_num']);
	$sql = "SELECT a.aid,a.username,a.title,a.urlname,a.dateline,b.urlname AS catename,b.cid AS cid FROM ".DB_PREFIX."article a JOIN ".DB_PREFIX."category b ON a.cid = b.cid WHERE a.status < 3 AND a.ischeck = 1 AND b.status = 0 ORDER BY a.dateline DESC $limit";
	$query = $_G['db']->query($sql);
	while ( $row = $_G['db']->fetch_array($query) ) {
		if ( $_G['settings']['seo']['rewrite'] == 1 ) {
			$row['url'] = 'article/';
			$row['url'] .= empty($row['urlname']) ? $row['aid'] : rawurlencode($row['urlname']);
			$row['url'] .= '/';
		} elseif ( $_G['settings']['seo']['rewrite'] == 2 ) {
			!$row['catename'] && $row['catename'] = $row['cid'];
			$row['url'] = get_html_dir($row['dateline'],$row['catename']).'/'.(empty($row['urlname']) ? $row['aid'] : rawurlencode($row['urlname'])).'.html';
		} else {
			$row['url'] = './?aid='.$row['aid'];
		}
		unset($row['urlname'],$row['catename'],$row['cid']);
		$content[] = $row;
	}
	$_G['cache']->set('lastest_article',$content,null);
}

//缓存推荐文章
function commend_article_recache() {
	global $_G;
	$content = array();
	$limit = ' LIMIT 0,'.intval($_G['settings']['display']['sidebar_num']);
	$sql = "SELECT a.aid,a.username,a.title,a.urlname,a.dateline,b.urlname AS catename,b.cid AS cid FROM ".DB_PREFIX."article a  JOIN ".DB_PREFIX."category b ON a.cid = b.cid WHERE a.status < 3 AND a.ischeck = 1 AND a.iscommend = 1 AND b.status = 0 ORDER BY a.dateline DESC $limit";
	$query = $_G['db']->query($sql);
	while ( $row = $_G['db']->fetch_array($query) ) {
		if ( $_G['settings']['seo']['rewrite'] == 1 ) {
			$row['url'] = 'article/';
			$row['url'] .= empty($row['urlname']) ? $row['aid'] : rawurlencode($row['urlname']);
			$row['url'] .= '/';
		} elseif ( $_G['settings']['seo']['rewrite'] == 2 ) {
			!$row['catename'] && $row['catename'] = $row['cid'];
			$row['url'] = get_html_dir($row['dateline'],$row['catename']).'/'.(empty($row['urlname']) ? $row['aid'] : rawurlencode($row['urlname'])).'.html';
		} else {
			$row['url'] = './?aid='.$row['aid'];
		}
		unset($row['urlname'],$row['catename'],$row['cid']);
		$content[] = $row;
	}
	$_G['cache']->set('commend_article',$content,null);
}

//缓存最新评论
function lastest_comment_recache() {
	global $_G;
	$content = array();
	$limit = ' LIMIT 0,'.intval($_G['settings']['display']['sidebar_num']);
	$sql = "SELECT a.cid,a.uid,a.username,a.content,a.dateline,b.dateline AS posttime,a.ishide,b.title,b.urlname,c.urlname AS catename,c.cid AS cid FROM ".DB_PREFIX."comment a JOIN ".DB_PREFIX."article b ON a.referid = b.aid JOIN ".DB_PREFIX."category c ON b.cid = c.cid WHERE c.status = 0 AND a.status = 0 ORDER BY a.cid DESC $limit";
	$query = $_G['db']->query($sql);
	while ($row = $_G['db']->fetch_array($query)) {
		if ( $_G['settings']['seo']['rewrite'] ==1 ) {
			$row['url'] = 'article/';
			$row['url'] .= empty($row['urlname']) ? $row['aid'] : rawurlencode($row['urlname']);
			$row['url'] .= '/#comment-'.$row['id'];
		} elseif ( $_G['settings']['seo']['rewrite'] ==2 ) {
			!$row['catename'] && $row['catename'] = $row['cid'];
			$row['url'] = get_html_dir($row['posttime'],$row['catename']).'/'.(empty($row['urlname']) ? $row['aid'] : rawurlencode($row['urlname'])).'.html#comment-'.$row['id'];
		} else {
			$row['url'] = './?aid='.$row['aid'].'#comment-'.$row['id'];
		}
		if ($row['ishide']) $row['content'] = '这是一条隐藏评论';
		$row['content'] = cut_str(strip_tags($row['content']), $_G['settings']['display']['sidebar_charnum']);
		unset($row['urlname'], $row['ishide'], $row['catename'], $row['cid'], $row['posttime']);
		$content[] = $row;
	}
	$_G['cache']->set('lastest_comment',$content,null);
}

//缓存友情链接
function link_recache() {
	global $_G;
	$content = array();
	$sql = "SELECT c.cid,c.status,c.catename,l.title,l.url,l.logo,l.summary,l.rssurl,l.lid FROM ".DB_PREFIX."category c JOIN ".DB_PREFIX."link l ON l.cid = c.cid WHERE c.status <> 3 ORDER BY c.listorder ASC, l.listorder ASC";
	$query = $_G['db']->query($sql);
	while ($row = $_G['db']->fetch_array($query)) {
		$content[$row['url']] = array(
			'title' => $row['title'],
			'rssurl' => $row['rssurl'],
			'lid'  =>  $row['lid'],
			'catename' => $row['catename'],
			'logo' => $row['logo'],
			'status' => $row['status'],
			'summary' => $row['summary'],
		);
		
//		if ( !array_key_exists($row['cid'], $content) ) {
//			$content[$row['cid']] = array(
//				'catename' => $row['catename'],
//				'status' => $row['status'],
//				'links' => array()
//			);
//		}
//		$content[$row['cid']]['links'][] = array(
//			'title' => $row['title'],
//			'url' => $row['url'],
//			'logo' => $row['logo'],
//			'summary' => $row['summary'],
//		);
	}
	$_G['cache']->set('link',$content,null);
}

function nav_recache(){
	global $_G;
	$navs=$_G['db']->fetch('nav','*',null,'displayorder ASC');
	$_G['cache']->set('nav',$navs);
}
//缓存链接分组
function linkgroup_recache() {
	global $_G;
	$content = array();
	$query = $_G['db']->query("SELECT * FROM ".DB_PREFIX."linkgroup ORDER BY listorder");
	while ($row = $_G['db']->fetch_array($query)) {
		$content[$row['gid']] = array(
			'groupname' => $row['groupname'],
			'total' => $row['total'],
			'gid' => $row['gid'],
			'status' => $row['status']
		);
	}
	$_G['cache']->set('linkgroup',$content,null);
}

//缓存Tag标签
function tags_recache() {
	global $_G;
	$content = array();
	$sql = "SELECT tagid,tagname,`total` FROM ".DB_PREFIX."tags WHERE 1 ORDER BY tagid ASC";
	$query = $_G['db']->query($sql);
	while ($row = $_G['db']->fetch_array($query)) {
		$content[$row['tagid']] = array('tagname' => $row['tagname'],'total' => $row['total']);
	}
	$_G['cache']->set('tags',$content,null);
}

//缓存插件信息
function plugin_recache() {
	global $_G;
	$content = array();
	$sql = "SELECT * FROM ".DB_PREFIX."plugin";
	$query = $_G['db']->query($sql);
	while ($row = $_G['db']->fetch_array($query)) {
		$content[$row['identifier']] = array('id' => $row['id'],'name' => $row['name'],'directory' => $row['directory'],'enabled' => $row['enabled'], 'runmode' => $row['runmode']);
		write_to_file('plugin_'.$row['id'], $row);
	}
	$_G['cache']->set('plugin',$content,null);
}

//缓存自定义页面信息
function page_recache() {
	global $_G;
	$content = array();
	$sql = "SELECT id,pagename,filepath FROM ".DB_PREFIX."page WHERE ishide = 0 ORDER BY orderid";
	$content = $_G['db']->fetch_all($sql);
	$_G['cache']->set('page',$content,null);
}
?>